package com.atguigu.spzx.user.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.atguigu.spzx.common.util.AuthContextUtil;
import com.atguigu.spzx.common.util.IpUtil;
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "注册登录")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;


    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody  UserRegisterDto userRegisterDto){
        userInfoService.register(userRegisterDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody UserLoginDto loginDto, HttpServletRequest request){
        String ip = IpUtil.getIpAddress(request);
        String token = userInfoService.login(loginDto,ip);
        return Result.build(token,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
    }

//    @Operation(summary = "获取当前登录用户信息")
//    @GetMapping("/auth/getCurrentUserInfo")
//    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
//        String token = request.getHeader("token");
//        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);
//        return Result.build(userInfoVo,ResultCodeEnum.SUCCESS);
//    }
}
